﻿namespace FallingWordsGame
{
    using System;

    public class Keyboard
    {
        public ConsoleKeyInfo pressedKey;
        
        public void ProcessInput()
        {
            if(Console.KeyAvailable)
            {
                pressedKey = Console.ReadKey(true);

                if(char.IsLetter(pressedKey.KeyChar))
                {
                    if (LetterPressed != null)
                    {
                        LetterPressed(this, new EventArgs());
                    }
                }

                if(pressedKey.Key.Equals(ConsoleKey.Escape))
                {
                    Environment.Exit(0);
                }

                if(pressedKey.Key.Equals(ConsoleKey.Spacebar))
                {
                    if(SpacePressed != null)
                    {
                        SpacePressed(this, new EventArgs());
                    }
                }

                if (pressedKey.Key.Equals(ConsoleKey.Enter))
                {
                    if (EnterPressed != null)
                    {
                        EnterPressed(this, new EventArgs());
                    }
                }

                if(pressedKey.Key.Equals(ConsoleKey.UpArrow) || pressedKey.Key.Equals(ConsoleKey.DownArrow) ||
                   pressedKey.Key.Equals(ConsoleKey.LeftArrow) || pressedKey.Key.Equals(ConsoleKey.RightArrow))
                {
                    if(this.ArrowPressed != null)
                    {
                        this.ArrowPressed(this, new EventArgs());
                    }
                }
            }
        }

        public event EventHandler LetterPressed;
        public event EventHandler SpacePressed;
        public event EventHandler EnterPressed;
        public event EventHandler ArrowPressed;
    }
}
